// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import vwui from 'vwui'
Vue.use(vwui)

import routes from './router'
import sess from './sess'

const beforeEach = (toRoute, fromRoute, next)=>{
  console.log('00000')
  const to = toRoute.path
  const from = fromRoute.path
  let scrollTop = vwui.app.pageContentScrollTop()

  console.log(scrollTop)

  let h = sess.get(to)

  if(h && h.history){
    vwui.app.nextDirection('back')

    h.history = false
    sess.set(to, h)
  }else{
    vwui.app.nextDirection('forward')

    sess.set(from || '/', {
      history: true,
      scrollTop: scrollTop
    })
  }

  next()
}

const afterEach = (toRoute, fromRoute)=>{
  const to = toRoute.path
  const from = fromRoute.path

  const h = sess.get(to)
  if(h && h.scrollTop){
    Vue.nextTick(()=>{
      vwui.app.pageContentScrollTop(h.scrollTop)
    })
  }
}

const router = new VueRouter(routes)

router.beforeEach(beforeEach)
router.afterEach(afterEach)

Vue.use(vwui.app, {
  router
})
